package L3iob;

import java.io.*;

public class StudentsManager {
    public void Add_Students(int number,String name,String sex) {
        CheckNumber checkNumber = new CheckNumber();
        try {
            checkNumber.check(number);
            BufferedWriter bufferedWriter = null;
            try {
                String  str =number+" "+name+" "+sex;
                bufferedWriter = new BufferedWriter(new FileWriter("./src/main/resource/test.txt", true));
                bufferedWriter.write(str);
                bufferedWriter.newLine();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (bufferedWriter!=null){
                    try {
                        bufferedWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (MyException e) {
            e.printStackTrace();
        }

    }
    public void Read_Student(int num) {
        CheckNumber checkNum = new CheckNumber();
        try {
            checkNum.check(num);
            BufferedReader read = null;
            try {
                read = new BufferedReader(new FileReader("./src/main/resource/test.txt"));
                String s = null;
                boolean flag=true;
                while (flag){
                    try {
                        if (((s = read.readLine())!=null) && s.length()!=0) {
                            String [] str=s.split(" ");
                            if (str[0].equals(num)){
                                System.out.println("学号："+str[0]+"; 姓名："+str[1]+"; 性别："+str[2]);
                                break;
                            }
                        }
                        else {
                            flag=false;
                            System.out.println("没有这位同学的信息！");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            try {
                read.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MyException e) {
            e.printStackTrace();
        }

    }
    public void Read_Students(){
        BufferedReader read = null;
        try {
            read = new BufferedReader(new FileReader("./src/main/resource/test.txt"));
            String s = null;
            while (true){
                try {
                    if (!((s = read.readLine())!=null)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String [] str=s.split(" ");
                if(str.length!=1){
                    System.out.println("学号："+str[0]+"; 姓名："+str[1]+"; 性别："+str[2]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(read!=null){
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
